package com.example.everying.controller.school;/*
 * 项目名称: everything
 * 文件名: SchoolController
 * 作者: 张泽阳
 * 日期: 2025/5/19
 * 描述:
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.everying.context.UserContext;
import com.example.everying.model.dto.school.GetSchoolByIdDto;
import com.example.everying.model.dto.school.SchoolPageDto;
import com.example.everying.model.entity.school.SchoolEntity;
import com.example.everying.model.entity.user.UserEntity;
import com.example.everying.service.school.SchoolService;
import com.example.everying.service.user.UserService;
import com.example.everying.utils.ImageUpload;
import com.example.everying.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @Autowired
    private ImageUpload imageUpload;

    @Autowired
    private UserService userService;

    @PostMapping("/list")
    public Result getSchoolList(@RequestBody SchoolPageDto schoolPageDto){
        // 分页查询
        Page<SchoolEntity> pageParam = new Page<>(schoolPageDto.getPage(), schoolPageDto.getLimit());
        IPage<SchoolEntity> pageModel = schoolService.selectPage(pageParam, schoolPageDto);
        return Result.ok(pageModel);
    }

    @PostMapping("/update")
    public Result addSchool(@RequestBody SchoolEntity schoolEntity){

        Result<?> result = schoolService.updateSchool(schoolEntity);
        // boolean save = schoolService.save(schoolEntity);
        return result;
    }

    @PostMapping("/getSchoolById")
    public Result getSchoolById(@RequestBody GetSchoolByIdDto getSchoolByIdDto){

        return schoolService.querySchoolById(getSchoolByIdDto.getId());
    }

    @PostMapping("/upload")
    public Result<?> upload(@RequestParam("files") MultipartFile[] file) throws IOException {
        UserEntity user = UserContext.getUser();
        if (user.getIsAdministrator() != 3){
            return Result.fail("权限不足");
        }
        List<String> list =imageUpload.uploadImages(file);
        return Result.ok(list);
    }


}
